// pages/storeList/storeList.js
const {
	globalData
} = getApp()
// import utoolsData from './utoolsData';
Component({
	/**
	 * 页面的初始数据
	 */
	data: {
		translateX: 198,
		currentTabIndex: 0,
		tabList: [{
				index: 0,
				name: '体验空间'
			},
			{
				index: 1,
				name: '免税门店'
			},
		],

		listData: [],
		// cityList:['全部门店'],
		cityList: ['全部门店'],
		utoolsData: {},
		dutyFreeShopData: {},
		navBarHeight: globalData.navBarHeight,
		defaultKeyword: '全部门店', //城市数据
		apiFunctionList: {} //从父组件传进来的api列表
		// navBarHeight:84
	},
	properties: {
		apilist: Function,
		tip: Function
	},
	async ready() {
		this.data.apiFunctionList = this.properties.apilist()
		// await this.storeList()
		let cityList = ['全部门店']
		await this.getDutyFreeShop()
		this.getStoreJson()
		this.data.dutyFreeShopData.children.map(item => {
			cityList.push(item.label1)
		})
		cityList = [...new Set(cityList)];
		this.setData({
			cityList,
		})
	},
	methods: {
		storeList: async function(jwt) {
			let that = this;
			let customerInfo = wx.getStorageSync('customerInfo') || jwt;
			let merchantInfo = wx.getStorageSync('merchantInfo')
			const parmas = {
				merchantId: merchantInfo.cloudMerchantId,
				page: 1,
				size: 50,
				storeField: this.data.defaultKeyword
			};
			const json = await this.data.apiFunctionList.storeAddressList({
				query: parmas
			});
			if (json.code == 1000) {
				this.data.listData = [...json.data.content];
				if (json.data.content.length == 0) {
					that.is_empty = true;
					that.showmessageSeach = true;
					// 搜索错误提示
					if (!!parmas.storeField) {
						that.searchError = true;
						that.is_searchEmpty = true;
						this.data.listData = json.data.content
					} else {
						that.searchError = false;
						that.is_searchEmpty = false;
					}
				}
				that.showLoaded = false;
				// 处理数据，去掉空格，去掉AM，将~替换成-
				this.data.listData.map(item => {
					if (item.storeBusTime) {
						item.storeBusTime = item.storeBusTime.replace(/\s+/g, '').replace('AM', '')
							.replace('PM', '').replace(/~/g, '-');
					}
				})
				this.setData({
					listData: this.data.listData
				})
				let systemInfo = wx.getStorageSync('systemInfo');
				let isIos = systemInfo.system.indexOf('iOS')
			} else {
				this.properties.tip().plainText(json.msg);
			}
		},
		//免税门店json
		async getDutyFreeShop() {
			const json = await this.data.apiFunctionList.getUtoolsJson({
				type: 'dutyFreeShop'
			});
			this.setData({
				dutyFreeShopData: JSON.parse(json.data)
			})
		},
		//门店空间数据
		async getStoreJson() {
			const json = await this.data.apiFunctionList.getUtoolsJson({
				type: 'experienceSpace'
			});
			this.setData({
				utoolsData: JSON.parse(json.data)
			})
		},
		tabClick(e) {
			const {
				target: {
					dataset: {
						index
					}
				}
			} = e
			let {
				currentTabIndex,
				translateX
			} = this.data
			let flag = index - currentTabIndex
			if (currentTabIndex == index) {
				return
			} else {
				this.setData({
					currentTabIndex: index,
					translateX: flag > 0 ? translateX + 328 : translateX - 328
				})
				if (flag > 0) {
					this.setData({
						defaultKeyword: '全部门店'
					}, () => {
						this.getDutyFreeShop()
					})
				}
			}
		},
		openCicyModal() {
			
		},
		bindPickerChange: async function(e) {
			await this.getDutyFreeShop()
			if (this.data.cityList[e.detail.value] == '全部门店') {
        this.setData({
          defaultKeyword:this.data.cityList[e.detail.value],
        })
				return
			}
			let filterData = this.filterShopData(this.data.cityList[e.detail.value])
			this.setData({
        defaultKeyword:this.data.cityList[e.detail.value],
				['dutyFreeShopData.children']: filterData
			})
		},
		filterShopData(key) {
			return this.data.dutyFreeShopData.children.filter((item) => item.label1 == key)
		}

	},
})